<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>练习4：五一倒计时</title>
</head>
<body onload="mydate()">
<div>
    <strong>距离五一还有：</strong>
    <span id="date"></span>
</div>


<script>
    function mydate() {
        var endTime = new Date('2021/5/1 00:00:00');//截止时间
        var nowTime = new Date();//现在时间距离1970年毫秒数
        var time = (endTime.getTime() - nowTime.getTime()) / 1000;

        var day = Math.floor(time / (24 * 60 * 60));
        var hours = Math.floor(time / (60 * 60) % 24);
        var minutes = Math.floor(time / 60 % 60);
        var seconds = Math.floor(time % 60);

        var day = addZero(day);
        var hours = addZero(hours);
        var minutes = addZero(minutes);
        var seconds = addZero(seconds);

        document.getElementById("date").innerHTML = day + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
        //setTimeout(function (){mydate()},1000);
        setInterval(mydate, 1000);
    }

    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
        //return i<10? "0" + i+"";
    }
</script>
</body>
</html>